package com.example.myfirstapplication.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class CommentRecyclerAdapter : RecyclerView.Adapter<CommentRecyclerAdapter.MyViewHolder>() {

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    private var mBindView: ((holder: MyViewHolder, data: Any?) -> Unit)? = null
    private var mLayout: Int? = null
    private var mDatalist: List<*>? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        //   val view = LayoutInflater.from(p0.context).inflate(mLayoutId!!, null, false)
        return (MyViewHolder(
            LayoutInflater.from(parent.context).inflate(mLayout!!, null, false)
        ))
    }

    override fun getItemCount(): Int {
        return mDatalist!!.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        mBindView?.invoke(
            holder, mDatalist?.get(position)
        ) //在onBindView方法里面实现绑定
    }

    class Builder {
        private var commentAdapter: CommentRecyclerAdapter = CommentRecyclerAdapter()
        fun setData(data: List<*>): Builder {
            commentAdapter.mDatalist = data
            return this
        }

        fun setLayout(layout: Int): Builder {
            commentAdapter.mLayout = layout
            return this
        }

        fun bindView(bindView: ((holder: MyViewHolder, data: Any?) -> Unit)): Builder {
            commentAdapter.mBindView = bindView
            return this
        }

        fun create(): CommentRecyclerAdapter {
            return commentAdapter
        }
    }
}
